package com.dhj;


import org.jsoup.Jsoup;
import org.jsoup.nodes.Attributes;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.File;
import java.util.Set;

/**
 * @author DaHuaJia
 * @Description 选择器
 * @Date 2021-11-13 13:58:34
 */
public class TestSelector {


    /**
     * tagname 通过标签查找元素
     * #id 通过ID查找元素
     * .class 通过class查找元素
     * [attribute] 通过属性查找元素
     * [attr=value] 通过属性值来查找元素
     */


    public static void main(String[] args) throws Exception {
        Document doc = Jsoup.parse(new File("D:\\FreeSpace\\2021\\crawler\\secondCrawler\\src\\main\\resources\\ningmeng.html"), "utf8");

        // tagName
        Elements elements = doc.select("title");
        for (Element ele : elements){
            System.out.println("title="+ele.text());
        }

        // #id
        Elements ids = doc.select("#keyId");
        for (Element ele : ids){
            System.out.println("keyId="+ele.id());
        }

        // .class
        Elements classs = doc.select(".keyClass");
        for (Element ele : classs) {
            System.out.println("keyClass=" + ele.className());
        }

        // [attribute]
        Elements attrs = doc.select("[key2]");
        for (Element ele : attrs) {
            System.out.println("attribute=" + ele.attributes().toString());
        }

        // [attr=value]
        Elements attr = doc.select("[key2=keyKey2]");
        for (Element ele : attr) {
            System.out.println("attrValue=" + ele.attributes().toString());
        }

    }

}
